package cn.chh.filter;

import java.io.*;
import javax.servlet.*;

/**
 * 编码拦截器
 * 
 * @author 华s
 */
public class EncodingFilter implements Filter {
	// 初始化用来接收编码的字符串
	String encoding;

	/**
	 * 从web-xml中读取初始化信息进行初始化
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		encoding = filterConfig.getInitParameter("encode");
	}
	
	/**
	 * 进行请求拦截
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		try {
			//对request和respond的请求进行编码拦截
			request.setCharacterEncoding(encoding);
			response.setContentType("text/html;charset=encoding");// 设置传输编码
		} catch (Exception e) {
			System.out.println("error");
		}

		chain.doFilter(request, response);
	}
	
	/**
	 * 过滤器销毁
	 */
	public void destroy() {
		// System.out.println("** 过滤器销毁...") ;
	}
};
